<?php

namespace app\common\service;

use app\common\model\UserModel;

class LoginService extends BaseService
{
    public function userLogin($user)
    {
        //token登录
        $tokenService = new TokenService();
        $token = $tokenService->createToken($user['id']);

        //session登录
        session('user', $user);

        return [$user, $token];
    }

    public function userLogout()
    {
        session('user', null);
    }

    /**
     * 账号密码登录
     * @param $account string 账号
     * @param $password string 密码
     * @param $account_type string 账号类型
     * @return array
     */
    public function login($username, $password)
    {

        $userModel = new UserModel();
        $user = $userModel->where('username', $username)->find();
        if ( !$user ) {
            retError('账号或密码错误');
        }

        if ( $user['password'] != xn_encrypt($password, $user['salt']) ) {
            retError('账号或密码错误');
        }

        if ( $user['status'] != 1 ) {
            retError('账号被禁用');
        }

        // 更新最后登录时间
        $user->last_login_time = time();
        $user->save();

        return $this->userLogin($user);
    }

    public function register($data)
    {
        $userModel = new UserModel();

        $account_type = xn_cfg('base.account_type', 'username'); //默认账号类型
        if ( $account_type == 'mobile') {
            if ( preg_match('/^1[3456789]\d{9}$/', $data['username']) == false ) {
                retError('手机号码不正确');
            }
        } elseif ( $account_type == 'email') {
            if ( filter_var($data['username'], FILTER_VALIDATE_EMAIL) == false ) {
                retError('邮箱不正确');
            }
        } else {
            if ( strlen($data['username']) < 2 ) {
                retError('账号不能少于2位');
            }
        }

        // 判断账号是否已存在
        if ( $userModel->where('username', $data['username'])->find() ) {
            retError('账号已存在');
        }

        // 判断手机号是否已存在
        if ( $data['mobile'] != '' ) {
            if ( $userModel->where('mobile', $data['mobile'])->find() ) {
                retError('手机号已存在');
            }
        }

        // 判断邮箱是否已存在
        if ( $data['email'] != '' ) {
            if ( $userModel->where('email', $data['email'])->find() ) {
                retError('邮箱已存在');
            }
        }

        // 昵称
        if ( $data['nickname'] == '' ) $data['nickname'] = $data['username'];

        $data['password'] = xn_encrypt($data['password'], $data['salt']);
        $user = $userModel->create($data);
        return $this->userLogin($user);
    }

    /**
     * 验证码登录/注册
     * @param $mobile
     * @param $password
     */
    public function yzmLogin($mobile, $password = '')
    {
        $userModel = new UserModel();
        //直接登录
        if( $user = $userModel->where('mobile', $mobile)->find() ) {
            return $this->userLogin($user);
        }
        //注册
        $user = $userModel->create(
            [
                'username' => $mobile,
                'mobile' => $mobile,
                'nickname' => substr(md5(uniqid(true)), 0, 6),
                'password' => $password!='' ? xn_encrypt($password) : '',
                'create_time' => time(),
            ]
        );
        return $this->userLogin($user);
    }
}